<?php
/**
 * Created by PhpStorm.
 * DateTime: 2020/10/24 0:03
 * Intro:
 */

namespace addons\podunk\controller;


class User extends Base
{
    protected $noNeedLogin = ['login', 'accountLogin', 'logout'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        return parent::_initialize();
    }

    /**
     * 会员登录
     *
     * @param string $account 账号
     * @param string $password 密码
     */
    public function accountLogin()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    public function info()
    {
        $userInfo = $this->auth->getUserInfo();
        $userInfo['rules'] = $this->auth->getRuleList();
        $this->success('获取成功',$userInfo);
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }

}